//
//  TJEmployeeListController.m
//  BaoDian
//
//  Created by mac on 2024/7/27.
//

#import "TJStaffListController.h"
#import "TJStaffListCell.h"
#import "TJEmptyDataHandler.h"
#import "HJModifyGroupNameController.h"
#import "TJCreateStaffController.h"
#import "TJUIAlertController.h"
#import "TJClockInRecardController.h"

@interface TJStaffListController ()

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;
@property (nonatomic, weak) UIButton *createButton;

@end

@implementation TJStaffListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"员工列表";
    
    UIButton *createButton = [UIButton buttonWithSuperView:self.view font:UIFontMake(16) color:UIColor.whiteColor title:[NSString stringWithFormat:@"创建员工%d/%@", self.model.userTotal.intValue -  self.model.userRemaining.intValue, self.model.userTotal]];
    createButton.addCornerRadius(22);
    createButton.backgroundColor = UIColorMake(0x2978F0);
    [createButton addTarget:self action:@selector(didClickCreateButton) forControlEvents:UIControlEventTouchUpInside];
    [createButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.inset(TabbarSafeBottomMargin + 10);
        make.left.right.inset(16);
        make.height.offset(44);
    }];
    
    [self.tableView registerClass:TJStaffListCell.class forCellReuseIdentifier:@"TJEmployeeListCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 262;
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.contentOffset = CGPointMake(0, -16);
    self.tableView.backgroundColor = self.view.backgroundColor;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.offset(0);
        make.bottom.equalTo(createButton.mas_top).inset(10);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.text = @"你暂时还没有创建分组";
        dataModel.iconName = @"EmployeeManageEmpty";
        return TJEmptyDataTypeBase;
    }];
    
    _createButton = createButton;
    
    [self loadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJStaffListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJEmployeeListCell" forIndexPath:indexPath];
    TJStaffListModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickDeleteButtonBlock:^{
       
        TJUIAlertController *alert = [TJUIAlertController alertControllerWithTitle:nil message:@"您是否确认删除该员工账号，删除后不可恢复，请谨慎操作?" preferredStyle:TJUIAlertControllerStyleActionSheet];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"删除" style:TJUIAlertActionStyleDestructive handler:^(TJUIAlertAction * _Nonnull action) {
            
            [TJHUD showLoadingInView:weakself.view];
            [TJMinePresent deleteStaffWithUserId:model.userId complete:^(NSError * _Nullable error) {
                if (error) {
                    return;
                }
                
                weakself.model.userRemaining = @(weakself.model.userRemaining.intValue - 1);
                [weakself.createButton setTitle:[NSString stringWithFormat:@"创建员工%d/%@", weakself.model.userTotal.intValue -  weakself.model.userRemaining.intValue, weakself.model.userTotal] forState:UIControlStateNormal];
                if (weakself.createStaffSuccess) {
                    weakself.createStaffSuccess();
                }
                
                [weakself.datas removeObject:model];
                [weakself.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }];
            
        }]];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"取消" style:TJUIAlertActionStyleCancel handler:^(TJUIAlertAction * _Nonnull action) {
            
        }]];
        [alert showInViewController:weakself];
    }];
    
    [cell setDidClickDisableButtonBlock:^{
        
        if (model.status.intValue == 0) {//正常显示禁用
            
            TJUIAlertController *alert = [TJUIAlertController alertControllerWithTitle:nil message:@"您是否确认禁止该员工登录，禁止登录后可以点击恢复 登录即可重新给账号开放登录?" preferredStyle:TJUIAlertControllerStyleActionSheet];
            [alert addAction:[TJUIAlertAction actionWithTitle:@"禁止" style:TJUIAlertActionStyleDestructive handler:^(TJUIAlertAction * _Nonnull action) {
                
                [TJHUD showLoadingInView:weakself.view];
                [TJMinePresent disableStaffWithUserId:model.userId complete:^(NSError * _Nullable error) {
                    if (error) {
                        return;
                    }
                    model.status = @2;
                    [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                }];

            }]];
            [alert addAction:[TJUIAlertAction actionWithTitle:@"取消" style:TJUIAlertActionStyleCancel handler:^(TJUIAlertAction * _Nonnull action) {
                
            }]];
            [alert showInViewController:weakself];
        } else if (model.status.intValue == 2){//禁用显示启用
            
            TJUIAlertController *alert = [TJUIAlertController alertControllerWithTitle:nil message:@"您是否确认启用该员工登录，启用即可重新给账号开放登录?" preferredStyle:TJUIAlertControllerStyleActionSheet];
            [alert addAction:[TJUIAlertAction actionWithTitle:@"启用" style:TJUIAlertActionStyleDestructive handler:^(TJUIAlertAction * _Nonnull action) {
                
                [TJHUD showLoadingInView:weakself.view];
                [TJMinePresent enableStaffWithUserId:model.userId complete:^(NSError * _Nullable error) {
                    if (error) {
                        return;
                    }
                    model.status = @0;
                    [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                }];

            }]];
            [alert addAction:[TJUIAlertAction actionWithTitle:@"取消" style:TJUIAlertActionStyleCancel handler:^(TJUIAlertAction * _Nonnull action) {
                
            }]];
            [alert showInViewController:weakself];
        }
    }];
    
    [cell setDidClickClockInButtonBlock:^{
        TJClockInRecardController *employeeListController = [[TJClockInRecardController alloc] init];
        [weakself.navigationController pushViewController:employeeListController];
    }];
    return cell;
}

- (void)didClickCreateButton {
    TJCreateStaffController *createEmployeeController = [[TJCreateStaffController alloc] init];
    createEmployeeController.groupId = self.model.groupId;
    [createEmployeeController setComplete:^{
        [self loadData];
        self.model.userRemaining = @(self.model.userRemaining.intValue - 1);
        [self.createButton setTitle:[NSString stringWithFormat:@"创建员工%d/%@", self.model.userTotal.intValue -  self.model.userRemaining.intValue, self.model.userTotal] forState:UIControlStateNormal];
        if (self.createStaffSuccess) {
            self.createStaffSuccess();
        }
    }];
    [self presentViewController:createEmployeeController animated:NO completion:nil];
}
 

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


- (void)loadData {
    [TJMinePresent laodStaffListWithPageIndex:1 groupId:self.model.groupId complete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        if (datas.count) {
            [self.datas removeAllObjects];
            [self.datas addObjectsFromArray:datas];
        }
        self.emptyDataHandler.canDisplay = YES;
        [self.tableView reloadData];
    }];
}
 

@end
